/**
 * 
 */
package rs.fon.rapidminer.operator.learner.tree;

import java.util.LinkedList;
import java.util.List;

import com.rapidminer.example.Attribute;
import com.rapidminer.example.Example;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.set.SortedExampleSet;
import rs.fon.rapidminer.operator.learner.tree.SplittedExampleSet;


import rs.fon.rapidminer.process.annotation.Parameter;

/**
 * @author Nikola
 * 
 */

public class BinaryNumericalPossibleSplit extends AbstractPossibleSplit{
	

	
	
	
	
	@Override
	public Object DoWork(ExampleSet exampleSet, List<rs.fon.rapidminer.process.Parameter> parameters, boolean [] attributesForSplitting, double insignificanceParameter, int depth)
    {       
        List<SplittedExampleSet> sviSplitedi = new LinkedList<SplittedExampleSet>();
       
        
        
        int i=0;
      
        
        
        for(Attribute attribute : exampleSet.getAttributes())
        {
        	if (!attribute.isNominal()){
        		
        		if(attributesForSplitting[i])
        		{
                     ExampleSet trainingSet = (ExampleSet)exampleSet.clone();
                     List<SplittedExampleSet>  binarySplitList = this.Split(trainingSet, attribute);        
                      sviSplitedi.addAll(binarySplitList);
        		}
        	}
        	i++;
        }
       
        return sviSplitedi;
    }
	public  LinkedList<SplittedExampleSet> Split(ExampleSet inputSet, Attribute attribute) {
        SortedExampleSet exampleSet = new SortedExampleSet((ExampleSet)inputSet.clone(), attribute, SortedExampleSet.INCREASING);
        Attribute labelAttribute = exampleSet.getAttributes().getLabel();
        double oldLabel = Double.NaN;
        double bestSplit = Double.NaN;
        double lastValue = Double.NaN;;
        double bestSplitBenefit = Double.NEGATIVE_INFINITY;
        int counter = -1;
        Example lastExample = null;
        LinkedList<SplittedExampleSet> binarySplitList  =  new LinkedList<SplittedExampleSet>();
       
        for (Example e : exampleSet) {
            counter++;
        	double currentValue = e.getValue(attribute);
        	double label = e.getValue(labelAttribute);      // Izlaz Play Yes-No      
    		if(Double.isNaN(lastValue))
    			lastValue=currentValue;
    		 if ((Double.isNaN(oldLabel)) || (oldLabel != label)) {
        		double splitValue = (lastValue + currentValue) / 2.0d;
        		SplittedExampleSet splitted = SplittedExampleSet.splitByAttribute(exampleSet, attribute, splitValue);
        		splitted.setAttribute(attribute);
        		splitted.setSplitValue(splitValue);
        		binarySplitList.add(splitted);
        		oldLabel = label;
        	}
            lastValue = currentValue;
        }
        return binarySplitList;
    }

	
}
